import http from '../../../utils/https.js';

Page({
  data: {
    activity: {
      id: '',
      title: '',
      source: '',
      time: '',
      desc: '',
      views: 0,
      image: ''
    }
  },

  onLoad(options) {
    const activityId = options.id;
    this.fetchActivityDetails(activityId);
  },

  // 获取活动详情
  async fetchActivityDetails(id) {
    try {
      wx.showLoading({
        title: '加载中...',
        mask: true
      });
      
      const res = await http.get(`activity/activity/detail/${id}`);
      
      if (res.code === 200) {
        const item = res.data;
        // 格式化日期
        const formattedDate = this.formatDate(item.submitDate);
        
        this.setData({
          activity: {
            id: item.id,
            title: item.activityTitle,
            source: item.organizer || '物业服务中心',
            time: formattedDate,
            desc: item.activityDetail,
            views: item.viewCount || 0,
            image: item.activityImage || '/images/default-detail.jpg'
          }
        });
      } else {
        wx.showToast({
          title: res.msg || '获取详情失败',
          icon: 'none'
        });
      }
    } catch (err) {
      console.error('请求失败', err);
      wx.showToast({
        title: err.message || '网络异常',
        icon: 'none'
      });
    } finally {
      wx.hideLoading();
    }
  },

  // 日期格式化方法
  formatDate(dateString) {
    if (!dateString) return '';
    try {
      const date = new Date(dateString);
      return `${date.getFullYear()}/${(date.getMonth() + 1).toString().padStart(2, '0')}/${date.getDate().toString().padStart(2, '0')}`;
    } catch (e) {
      return dateString.substring(0, 10); // 简单截取
    }
  }
});